package cn.stylefeng.guns.rabbitmq.producer;

import cn.stylefeng.guns.rabbitmq.RabbitConstant;
import cn.stylefeng.guns.rabbitmq.vo.MessageVo;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessagePostProcessor;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.UUID;

/**
 * @author jj
 * @date 2019/12/9 15:20
 */
@Component
@Slf4j
public class Sender implements RabbitTemplate.ConfirmCallback, RabbitTemplate.ReturnCallback {

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @PostConstruct
    public void init() {
        rabbitTemplate.setConfirmCallback(this);
        rabbitTemplate.setReturnCallback(this);
    }

    //消息发送确认回调方法
    @Override
    public void confirm(CorrelationData correlationData, boolean ack, String cause) {
        log.info("消息发送成功:" + correlationData);
    }

    //消息发送失败回调方法
    @Override
    public void returnedMessage(Message message, int replyCode, String replyText, String exchange, String routingKey) {
        log.error("消息发送失败:" + new String(message.getBody()));
    }

    /**
     * 发送消息，不需要实现任何接口，供外部调用 挂售消息
     *
     * @param messageVo
     */

    public void sendBuy(MessageVo messageVo) {
        CorrelationData correlationId = new CorrelationData(UUID.randomUUID().toString());
        rabbitTemplate.convertAndSend(RabbitConstant.EXCHANGE,RabbitConstant.RK_CONTRACT , JSONObject.toJSON(messageVo).toString(), correlationId);
    }

    /**
     * 发送消息，不需要实现任何接口，供外部调用 挂售消息
     *
     * @param messageVo
     */

    public void sendSell(MessageVo messageVo) {
        CorrelationData correlationId = new CorrelationData(UUID.randomUUID().toString());
        rabbitTemplate.convertAndSend(RabbitConstant.EXCHANGE,RabbitConstant.RK_CONTRACT , JSONObject.toJSON(messageVo).toString(), correlationId);
    }

    /**
     * 支付倒计时，不需要实现任何接口，供外部调用
     *
     * @param messageVo
     */


    public void sendMsg(MessageVo messageVo) {
        CorrelationData correlationId = new CorrelationData(UUID.randomUUID().toString());
        MessagePostProcessor messagePostProcessor = message -> {
            MessageProperties messageProperties = message.getMessageProperties();
//            设置编码
            messageProperties.setContentEncoding("utf-8");
//            设置过期时间10*1000毫秒
            messageProperties.setExpiration(messageVo.getTimestamp());
            return message;
        };
        rabbitTemplate.convertAndSend(RabbitConstant.XIEXCHANGE,RabbitConstant.RK_DISLETTER , JSONObject.toJSON(messageVo).toString(), messagePostProcessor);
    }


    /**
     * 确认倒计时，不需要实现任何接口，供外部调用
     *
     * @param messageVo
     */


    public void sendQrMsg(MessageVo messageVo) {
        CorrelationData correlationId = new CorrelationData(UUID.randomUUID().toString());
        MessagePostProcessor messagePostProcessor = message -> {
            MessageProperties messageProperties = message.getMessageProperties();
//            设置编码
            messageProperties.setContentEncoding("utf-8");
//            设置过期时间10*1000毫秒
            messageProperties.setExpiration(messageVo.getTimestamp());
            return message;
        };
        rabbitTemplate.convertAndSend(RabbitConstant.XIEXCHANGE,RabbitConstant.RK_AFFIRM , JSONObject.toJSON(messageVo).toString(), messagePostProcessor);
    }



    /**
     * 确认倒计时，不需要实现任何接口，供外部调用
     *
     * @param messageVo
     */


    public void sendJCMsg(MessageVo messageVo ,int type,int isVip) {
        MessagePostProcessor messagePostProcessor = message -> {
            MessageProperties messageProperties = message.getMessageProperties();
//            设置编码
            messageProperties.setContentEncoding("utf-8");
//            设置过期时间10*1000毫秒
            messageProperties.setExpiration(messageVo.getTimestamp());
            return message;
        };
        switch (type){
            case 0:
                if (isVip==1){
                    rabbitTemplate.convertAndSend(RabbitConstant.BUILDEXCHANGE,RabbitConstant.RK_BUILDING_XS_VIP , JSONObject.toJSON(messageVo).toString(), messagePostProcessor);
                }else {
                    rabbitTemplate.convertAndSend(RabbitConstant.BUILDEXCHANGE,RabbitConstant.RK_BUILDING_XS , JSONObject.toJSON(messageVo).toString(), messagePostProcessor);
                }
                break;
            case 1:
                if (isVip==1){
                    rabbitTemplate.convertAndSend(RabbitConstant.BUILDEXCHANGE,RabbitConstant.RK_BUILDING_CU_VIP , JSONObject.toJSON(messageVo).toString(), messagePostProcessor);
                }else {
                    rabbitTemplate.convertAndSend(RabbitConstant.BUILDEXCHANGE,RabbitConstant.RK_BUILDING_CU , JSONObject.toJSON(messageVo).toString(), messagePostProcessor);
                }
                break;
            case 2:
                if (isVip==1){
                    rabbitTemplate.convertAndSend(RabbitConstant.BUILDEXCHANGE,RabbitConstant.RK_BUILDING_ZH_VIP , JSONObject.toJSON(messageVo).toString(), messagePostProcessor);
                }else {
                    rabbitTemplate.convertAndSend(RabbitConstant.BUILDEXCHANGE,RabbitConstant.RK_BUILDING_ZH , JSONObject.toJSON(messageVo).toString(), messagePostProcessor);
                }
                break;
            case 3:
                if (isVip==1){
                    rabbitTemplate.convertAndSend(RabbitConstant.BUILDEXCHANGE,RabbitConstant.RK_BUILDING_GA_VIP , JSONObject.toJSON(messageVo).toString(), messagePostProcessor);
                }else {
                    rabbitTemplate.convertAndSend(RabbitConstant.BUILDEXCHANGE,RabbitConstant.RK_BUILDING_GA , JSONObject.toJSON(messageVo).toString(), messagePostProcessor);
                }
                break;
            default:
                log.error("参数错误！");
                break;
        }

    }
}